package L2;

import java.util.Scanner;

public class _002 {
	public static class Node{
		String startAddress;
		int data;
		String endAddress; 
		//Node next;
		public Node(String startAddress,int data,String endAddress/*,Node next*/)
		{
			this.startAddress=startAddress;
			this.data=data;
			this.endAddress=endAddress;
			//this.next=next;
		}
		public  Node()
		{
			this.startAddress="";
			this.data=0;
			this.endAddress="-1";
			//this.next=null;
		}
	}
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		String address=reader.next();
		int number=reader.nextInt();
		Node node[]=new Node[number];
		Node node1[]=new Node[number];
		Node node2[]=new Node[number];
		for(int i=0;i<node.length;i++)
		{
			node[i]=new Node();
			node1[i]=new Node();
			node2[i]=new Node();
			node[i].startAddress=reader.next();
			node[i].data=reader.nextInt();
			node[i].endAddress=reader.next();
		}
		int m=0,n=0;
		search(node,node1,node2,address,m,n);
		
		
		reader.close();

	}
	public static void search(Node[] node, Node[] node1, Node[] node2, String address,int m,int n) {
		for(int i=0;i<node.length;i++)
		{
			if(address==node[i].startAddress)
			{
				node1[m++]=node[i];
			}
			
		}
		
	}

}
